#include <scene.h>
#include <button.h>
#include <scene_manager.h>
#include <iostream>
#include <tilemap.h>
#include <tilemapgenerator.h>
#include <search/dfs.h>
#include <search/bfs.h>
#include <search/bfszx.h>
#include <generate/division_maze.h>
#include <generate/prim_maze.h>
#include <list_selecter.h>
#include <thread>
#include <win32_utils.h>
#include <randmap.h>

class SceneRandMap : public Scene{
private:
    SceneManager& sceneManager;
    std::unordered_map<std::string, Texture2D>& texture_blocks;
    Font* font;
    
    RandMap randmap;
    TileMapGenerator mapgener{10,10,7};
    TextButton btn_search{GetScreenWidth()-150,0,150,100,"search"};
    TextButton btn_save{GetScreenWidth()-150, 100*1, 150, 100, "save"};
    TextButton btn_testgener{GetScreenWidth()-150, 100*2, 150, 100, "gener"};
    ListSelecter blockSelceter{1500,4,64,64,60};

    Texture2D texture_loading = LoadTexture("assets\\textures\\loding.png");
    std::vector<Rectangle> frames;
    int frame=0;
    float timeout=0;
public:
    SceneRandMap(SceneManager& manager, std::unordered_map<std::string, Texture2D>& texture_blocks, Font* font) 
    :sceneManager(manager),texture_blocks(texture_blocks),font(font),randmap(texture_blocks){
        // mapgener{10,10,0};
        // tilemap.load(mapgener.generate_base());
        randmap.setDevMode(false);
        // tilemap.load("assets\\maze0.txt");
        btn_search.setOnClick([&](){
            randmap.search({1,1},{23,23},0,bfszx);
        });
        btn_save.setOnClick([&](){
            randmap.updateChunkTextureShaderDepth(randmap.getChunk({0,0}));
            // randmap.isRenderDepth = true;
        });
        btn_testgener.setOnClick([&](){
            randmap.generateStructure({0,0},{40,40},0, prim_maze);
        });

        // 初始化帧动画
        for(int j=0; j<3; j++)
            for(int i=0; i<4; i++)
                frames.push_back({(float)i*6,(float)j*6,6,6});

        blockSelceter.addActor(new ImageTextButton(texture_blocks["end"],"end"));
        blockSelceter.addActor(new ImageTextButton(texture_blocks["start"],"start"));
        blockSelceter.addActor(new ImageTextButton(texture_blocks["floor"],"floor"));
        blockSelceter.addActor(new ImageTextButton(texture_blocks["wall"],"wall"));
        blockSelceter.addActor(new ImageTextButton(texture_blocks["grass"],"grass"));
        blockSelceter.addActor(new ImageTextButton(texture_blocks["lava"],"lava"));
    };
    ~SceneRandMap() {}

    void init() override {
       randmap.init();
    }

    void render(float dt) override {
        timeout += dt;
        if(timeout > 0.1)
        {
            frame++;
            timeout = 0;
        }
        frame = (frame)%12;
        if(!randmap.getSearchStopped()) DrawTexturePro(texture_loading, frames[frame], {(float)GetScreenWidth()-150-100,0,100,100}, {0,0}, 0, WHITE);

        if(IsKeyPressed(KEY_F8)){
            if(randmap.getDevMode()) randmap.setDevMode(false);
            else randmap.setDevMode(true);
        }

        if(IsKeyPressed(KEY_F7)){
            if(randmap.getRestrictMode()) randmap.setRestictMode(false);
            else randmap.setRestictMode(true);
        }
        
        btn_search.render();
        btn_save.render();
        btn_testgener.render();

        randmap.render();

        // WinPressKey(0x5A);

        blockSelceter.render();
        
        float x= (GetMouseX()/randmap.camera.zoom -randmap.camera.offset.x/randmap.camera.zoom +randmap.camera.target.x) / 48;
        float y= (GetMouseY()/randmap.camera.zoom -randmap.camera.offset.y/randmap.camera.zoom +randmap.camera.target.y) / 48;
        if(IsMouseButtonDown(MOUSE_LEFT_BUTTON)){
            // std::cout << "zxionf:setblock at x = " << GetMouseX()/48/randmap.camera.zoom + randmap.camera.target.x/48 - randmap.camera.offset.x/48 << std::endl;
            
            // std::cout << x << "  " << y << std::endl;
            randmap.setBlock(x>=0?(int)x:x>-1?-1:(int)x-1, y>=0?(int)y:y>-1?-1:(int)y-1, blockSelceter.getActiveCustomIndex());
        }
        

        // DrawTextureEx(*(blockSelceter.getActive()->getTexture()), {GetMouseX()-(float)24,GetMouseY()-(float)24}, 0, 3, WHITE);

        randmap.drawInfo(2, GetScreenHeight()-32*8+4, 32, BLACK);
        randmap.drawInfo(0, GetScreenHeight()-32*8+2, 32, GREEN);

        drawKey("Z","zoom-",1500,800);
        drawKey("x","zoom+",1500,860);
        drawKey("E","next path",1500, 920);
        
    }

    void drawKey(const char* key, const char* jie, int x, int y){
        DrawRectangleRoundedLinesEx({(float)x,(float)y,48,48},0.1,10,4,BLACK);
        int width;
        width = MeasureText(key,32);
        DrawText(key,x+24-width/2,y+8+4,32,BLACK);
        DrawText(jie,x+56+18,y+8,32,BLACK);
    }
};